<?php
namespace app\index\controller;
use think\Controller;
use think\Exception;
use think\Request;

class Apps extends  Base
{
    public function lists()
    {
        $apps = new \app\index\model\Apps();
        $list=$apps->column("id,public_name,public_id,appid,secret");
        $this->assign("lists",$list);
        $this->assign("empty",'<tr><td class="center" colspan="5"> 没有数据</td></tr>');
        return $this->fetch();
    }
    public function step_01(){
        $action=input("action");
        if($action == 'edit' ){
            $id = input("pid");
            $app= new \app\index\model\Apps();
            $info = $app->where(["id"=>$id])->column("id,public_name,public_id,appid,secret,type");
            if(count($info)){
                $this->assign("current_app",$info[$id]);
            }

        }
        return $this->fetch();
    }

    public function step_02(){
        if($_POST){
            $result=[];
            try{
                $apps = new \app\index\model\Apps($_POST);
                $apps->save();
                session('app_new',intval($apps->id));
                $result["error"] = false;
                $result["errmsg"] = "sucess" ;

            }catch (\Exception $e){
                $result["error"] = 1;
                $result["errmsg"] = $e->getMessage() ;
            }
            return json($result);
        }else{
            $this->assign("app_new",session('app_new'));
            return $this->fetch();
        }
    }
    public function step_03(){
        if(!$_POST){
            return $this->fetch();
        }else{
            $public_info = $_POST;
            $id = session("app_new");


            try{
                if(!$id){
                    throw new \Exception("未检测到当前公众号信息，请尝试重新操作");
                }

                $apps = new \app\index\model\Apps();
                $apps->save($public_info,["id"=>$id]);
                $result["error"]=false;
                $result["errmsg"]="保存成功";
            }catch(\Exception $e){
                $result["error"] =true;
                $result["errmsg"] = $e->getMessage();
            }
            return json($result);

        }

    }
    public function uploadverify(){
        $result=[];
        $file = request()->file('verify_file');
        $old_file =null;
        try{
           if(!$file){throw new \Exception("没有检测到上传文件");}
            $info = $file->move(ROOT_PATH . 'public' ,'');
            if($info){
                $file = new \app\index\model\File();
                $data["mime"] = $info->getMime();
                $data["ext"] =$info->getExtension();
                $data["name"]=$info->getFilename();
                $data["savepath"] =$info->getPath();
                $data["size"] =$info->getSize();
                $data["md5"]=$info->md5();
                $data["sha1"]=$info->sha1();
                $data["location"]=$info->getRealPath();
                $data["create_time"] =time();

                $old_file=$file->where("md5",$info->md5())->find();
                if($old_file){
                    throw new \Exception("already");
                }
                $file->save($data);
                $data["id"]=intval($file->id);
                $result["error"] =false;
                $result["data"]= $data;
                $result["errmsg"] = "文件上传成功";

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }

        }catch (\Exception $e){
            $result["error"]=true;
            $result["errmsg"] =$e->getMessage();
            switch($e->getMessage()){
                case "already":
                    $result["errmsg"] ="该文件已经上传过了";
                    $result["data"] =$old_file;
                    break;
                default:
            }
        }
        return json($result);
    }

}
